﻿/********************************************************************************
** 作者： Mick
** 邮箱： zheng_jinfan@126.com
** 主页： http://www.zhengjinfan.cn
** 创始时间：2016-2-17
** 描述：
**      [辅助类]日志操作辅助类
*********************************************************************************/
using System;
using System.IO;
using System.Web.UI;

namespace BestEasyCMS.Framework.Util
{
    /// <summary>
    /// [辅助类]日志操作辅助类
    /// </summary>
    public class LogUtil
    {
        #region 构造实例

        /// <summary>
        /// 私有默认构造函数，禁止用户使用new()创建对象
        /// </summary>
        private LogUtil()
        {
        }

        /// <summary>
        /// 构建类的实例对象
        /// </summary>
        public static LogUtil Instance
        {
            get { return new LogUtil(); }
        }

        #endregion 构造实例
        /// <summary>
        /// 日志生成 -- 
        ///     文件目录：网站根目录下Logs文件夹
        /// </summary>
        /// <param name="msg"></param>
        public void Log(string msg)
        {
            var fileDir = new Page().Server.MapPath("~/Logs/log_" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt");

            var logFileInfo = new FileInfo(fileDir);
            if (logFileInfo.DirectoryName != null && !Directory.Exists(logFileInfo.DirectoryName))
            {
                if (logFileInfo.DirectoryName != null) Directory.CreateDirectory(logFileInfo.DirectoryName);
            }
            var fs = new FileStream(fileDir, FileMode.Append);
            var sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
            var logStr = string.Format("{0} # {1}", DateTime.Now, msg);
            sw.WriteLine(logStr);
            sw.Flush();
            sw.Close();
        }
    }
}
